<?php

namespace app\http\middleware;

//use Firebase\JWT\ExpiredException;
use Firebase\JWT\JWT;
use think\Exception;
use think\Request;

class CheckToken
{
//    protected $user_id = null;
    public function handle(Request $request, \Closure $next)
    {
        try {
            $params = $request->param();
            $secret = config('jwt.secret');
            $token  =  isset($params['token']) && $params['token']!=''?$params['token']:'';
            if($token=='') exit('token不能为空');
            $resObj = JWT::decode($token, $secret,['typ'=>'JWT','alg'=>'HS256']);
            //{ ["iss"]=> string(3) "sol" ["exp"]=> int(1587406453) ["aud"]=> string(11) "adminSystem"
            // ["nbf"]=> int(1586801653) ["iat"]=> int(1586801653) ["user_id"]=> int(101) }
            if($resObj->exp<time()) exit('token已失效请重新登录');
            $request->user_id = $resObj->user_id;
            return $next($request);
        } catch(\Exception $e) {
            echo print_r([$e->getCode(),$e->getMessage()]);exit();
        }

    }
}
